
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/account_router.dart';
import 'package:flutter_deer/account/iview/recharge_add_iview.dart';
import 'package:flutter_deer/account/models/user_bank_entity.dart';
import 'package:flutter_deer/account/presenter/recharge_add_present.dart';
import 'package:flutter_deer/account/provider/rechargs_add_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/dimens.dart';
import 'package:flutter_deer/res/gaps.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/util/change_notifier_manage.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/my_button.dart';
import 'package:flutter_deer/widgets/my_scroll_view.dart';
import 'package:flutter_deer/widgets/selected_item.dart';
import 'package:flutter_deer/widgets/text_field_item.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:provider/provider.dart';

class AddRechargePage extends StatefulWidget {

  const AddRechargePage({super.key});

  @override
  _AddRechargePageState createState() => _AddRechargePageState();
}

class _AddRechargePageState extends State<AddRechargePage> with BasePageMixin<AddRechargePage, PowerPresenter<dynamic>>, AutomaticKeepAliveClientMixin<AddRechargePage>,ChangeNotifierMixin<AddRechargePage> implements RechargeAddIMvpView {
  @override
  bool get wantKeepAlive => true;
  final TextEditingController _scoreController = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  RechargeAddProvider provider = RechargeAddProvider();
  late RechargeAddPresenter rechargeAddPresenter;
  String _cardNo = '';
  String _bankId = '';
  bool _clickable = false;

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    rechargeAddPresenter = new RechargeAddPresenter();
    powerPresenter.requestPresenter([rechargeAddPresenter]);
    return powerPresenter;
  }

  @override
  void saveRechargeSuccess(){
    NavigatorUtils.goBackWithParams(context, 'success');
  }

  @override
  void saveRechargeFail(){
    NavigatorUtils.goBackWithParams(context, 'fail');
  }

  void _verify() {
    bool clickable = true;
    String? score = _scoreController.text.nullSafe;
    if(score.isEmpty){
      clickable = false;
    }
    if(_bankId==null||_bankId==''){
      clickable = false;
    }
    if (clickable != _clickable) {
      setState(() {
        _clickable = clickable;
      });
    }
  }

  @override
  Map<ChangeNotifier, List<VoidCallback>?>? changeNotifier() {
    final List<VoidCallback> callbacks = <VoidCallback>[_verify];
    return <ChangeNotifier, List<VoidCallback>?>{
      _scoreController: callbacks,
      _nodeText1: null
    };
  }

  @override
  Widget build(BuildContext context) {
    final TextStyle? style = Theme.of(context).textTheme.titleSmall?.copyWith(fontSize: Dimens.font_sp14);
    return ChangeNotifierProvider<RechargeAddProvider>(
        create: (_) => provider,
        child:Scaffold(
          resizeToAvoidBottomInset: false,
          appBar: MyAppBar(
            title: '${DeerLocalizations.of(context)!.recharge.nullSafe}',
          ),
          body: MyScrollView(
              bottomButton: Padding(
                  padding: const EdgeInsets.only(left: 16.0, right: 16.0, bottom: 8.0),
                  child: Consumer<RechargeAddProvider>(
                      builder: (_,provider,child){
                        return  MyButton(
                          onPressed: _clickable?_submit:null,
                          text: '${DeerLocalizations.of(context)!.ok.nullSafe}',
                        );
                      }
                  )
              ),
              children: [
                Gaps.vGap5,
                TextFieldItem(
                  title: '${DeerLocalizations.of(context)!.rechargeAmount.nullSafe}',
                  keyboardType: TextInputType.number,
                  moneyFormmat: true,
                  hintText: '${DeerLocalizations.of(context)!.fillRechargeAmount.nullSafe}',
                  focusNode: _nodeText1,
                  controller: _scoreController,
                ),
                SelectedItem(
                  title: '${DeerLocalizations.of(context)!.bankCard.nullSafe}',
                  content: _cardNo.isEmpty ? '${DeerLocalizations.of(context)!.chooseBankCard.nullSafe}' : _cardNo,
                  style: _cardNo.isEmpty ? style : null,
                  onTap: () {
                    NavigatorUtils.pushResult(context, '${AccountRouter.userBankSelectPage}', (Object result) {
                      if(result!=null) {
                        setState(() {
                          UserBankEntity model = result as UserBankEntity;
                          _cardNo = model.cardNo.nullSafe;
                          _bankId = model.bankId ?? '';
                          _verify();
                        });
                      }
                    });
                  },
                ),
              ]
          ),
        )
    );
  }

  Future<void> _submit() async {
    rechargeAddPresenter.saveRecharge(_cardNo, _bankId, double.parse(_scoreController.text.replaceAll(',', '').nullSafe));
  }

}